package com.aw.baselib.net.callback

import com.aw.baselib.base.AwBaseView

/**
 * Created by hzw.
 */
abstract class AwApiCallback<T> : ApiBaseCallback<T> {
    private var mBaseView: AwBaseView?
    private var needLoading = true
    private var needCallbackFail = false

    constructor(baseView: AwBaseView?) {
        mBaseView = baseView
    }

    constructor(baseView: AwBaseView?, needCallbackFail: Boolean) {
        mBaseView = baseView
        this.needCallbackFail = needCallbackFail
    }

    constructor(baseView: AwBaseView?, needLoading: Boolean, needCallbackFail: Boolean) {
        mBaseView = baseView
        this.needLoading = needLoading
        this.needCallbackFail = needCallbackFail
    }

    override fun onStart() {
        if (needLoading && mBaseView != null) {
            mBaseView!!.showLoadingDialog()
        }
    }

    abstract override fun onSuccess(result: T)
    override fun onFail(code: Int, msg: String?) {
        if (!needCallbackFail && mBaseView != null) {
            mBaseView!!.showMsg(msg)
        }
        onCompleted()
    }

    override fun onCompleted() {
        if (needLoading && mBaseView != null) {
            mBaseView!!.dismissLoadingDialog()
        }
    }
}